import Vue from 'vue'
import App from './App'
import store from './store'


import headNavigation from './components/head-navigation.vue'
Vue.component('head-navigation', headNavigation)

import head1Navigation from './components/head1-navigation.vue'
Vue.component('head1-navigation', head1Navigation)

import modelUserlogin from './components/model-userlogin.vue'
Vue.component('model-userlogin', modelUserlogin)

import fixedNavigation from './components/fixed-navigation.vue'
Vue.component('fixed-navigation', fixedNavigation)

import pageLoading from './components/page-loading.vue'
Vue.component('page-loading', pageLoading)

Vue.prototype.$store = store

/** 帮助处理 **/
Vue.prototype.Util = {

};

/** 数据请求 **/
Vue.prototype.Http = {
	// 请求过渡状态
	loadModal: false,
	isLogin: 'CLOSE',
	// 数据接口地址
	requstUrl: "http://192.168.1.20:8081/duties/api/",

	loginOk: function() {
		console.log("登陆成功");
		this.isLogin = 'CLOSE';
	},
   
	// 获取Header
	acceptHeader: function() {
		let token = uni.getStorageSync('token');
		let header = {'content-type': 'application/x-www-form-urlencoded'};
		if(token) {
			header['token'] = token;
		}
		return header;
	},

	post: function(method, data, success, error) {
		const _this = this;
		
		// 过渡图标
		uni.showLoading({title: '加载中...'});
		return uni.request({
			url: _this.requstUrl + method,
			method: 'post',
			header: _this.acceptHeader(),
			data: data,
			success: (res) => {
				let responseData = res.data;
				
				// 拦截处理, 分情况处理
				if(responseData.returncode ) {
					if(responseData.returncode == "O" || responseData.returncode == "L") {
						uni.$emit("loginModel", 'OPEN');
					} else if(responseData.returncode == "N") {
						uni.showToast({
							title: responseData.returnmsg,
							icon: 'none',
							duration: 4000,
						});
					} else {
						success(res.data);
					}
				}
				
				// 没有拦截, 直接执行
				else {
					success(res.data);
				}
				uni.hideLoading();
			},
			error: (res) => {
				error(res.data);
			}
		});
	},
	
	get: function(method, data, success, error) {
		const _this = this;
		const _data = _this.transPath(data, true);
		_this.loadModal = true;
		return uni.request({
			url: _this.requstUrl + method + _data,
			method: 'get',
			success: (res) => {
				success(res.data);
				_this.loadModal = false;
			},
			error: (res) => {
				error(res.data);
				_this.loadModal = false;
			}
		});
	},
		
	transPath: function (data, isPrefix) {
		isPrefix = isPrefix ? isPrefix : false
		let prefix = isPrefix ? '/' : ''
		let _result = []
		for (let key in data) {
			let value = data[key]
			// 去掉为空的参数
			if (['', undefined, null].includes(value)) {
				continue
			}
			if (value.constructor === Array) {
				value.forEach(_value => {
					_result.push(encodeURIComponent(_value))
				})
			} else {
				_result.push(encodeURIComponent(value))
			}
		}
		return _result.length ? prefix + _result.join('/') : '';
	},
	
	transUrl: function (data, isPrefix) {
		isPrefix = isPrefix ? isPrefix : false
		let prefix = isPrefix ? '?' : ''
		let _result = []
		for (let key in data) {
			let value = data[key]
			// 去掉为空的参数
			if (['', undefined, null].includes(value)) {
				continue
			}
			if (value.constructor === Array) {
				value.forEach(_value => {
				_result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(_value))
				})
			} else {
				_result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))
			}
		}
		return _result.length ? prefix + _result.join('&') : ''
	}
};


Vue.prototype.Weixin = {

	// 微信授权
	login: function(_success, _fail, _userinfo) {
		let _this = this;
		
		// 因涉及APPID内容，发布组件不予通过，所以删除掉了，请自行完善。

	},
	
	// 微信授权
	auth: function(_success, _fail) {
		let _this = this;
		
		// 因涉及APPID内容，发布组件不予通过，所以删除掉了，请自行完善。
	},
	
	// APP跳转小程序
	launchMini: function() {
		if (plus.runtime.isApplicationExist({ pname: 'com.tencent.mm', action: 'weixin://' })) {
			// 唤醒微信小程序
			plus.share.getServices(function(res){
				var sweixin = null;  
				for(var i=0;i<res.length;i++){  
					var t = res[i];  
					if(t.id == 'weixin'){  
						sweixin = t;  
					}  
				}  
				if(sweixin){
					
					let token = uni.getStorageSync('token');
					if(token) {
						sweixin.launchMiniProgram({
							id: 'gh_bdfa72b192a2',
							path: 'pages/my/bind/bind?token=' + token,
							
							// 0-正式版； 1-测试版； 2-体验版
							type: 0
						});
					} 
					// Token不存在, 先登陆
					else {
						uni.$emit("loginModel", 'OPEN');
					}
				}  
			},function(res){  
				console.log(JSON.stringify(res));  
			});
		} else {
			uni.showToast({
			  title: '请先安装微信应用',
			  icon: 'none'
			});
		}

	},
	
	// 跳转虾芒小程序
	launchTrialMiniProgram: function(wePagePath) {
		uni.navigateToMiniProgram({
			appId: "wx8c5212557008a604",
			path: wePagePath,
			success: res => {
				// 打开成功
				console.log("打开成功", res);
			},
			fail: err => {
	
			}
		});
	}
};

Vue.prototype.Util = {
	btnAudio: function() {
		var music = uni.createInnerAudioContext();
		music.src= "/static/audio/10010.wav";
		music.play();
	},
	nextAudio: function() {
		var music = uni.createInnerAudioContext();
		music.src= "/static/audio/13216.wav";
		music.play();
	},
	successAudio: function() {
		var music = uni.createInnerAudioContext();
		music.src= "https://zuotiyoujiang.oss-cn-beijing.aliyuncs.com/guess_words/audio/4221.wav";
		music.play();
	},
	failAudio: function() {
		var music = uni.createInnerAudioContext();
		music.src= "https://zuotiyoujiang.oss-cn-beijing.aliyuncs.com/guess_words/audio/13157.wav";
		music.play();
	}

}

Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
	store,
	...App
})
app.$mount()
